/***************************************************************
 *
 * (c) 2008 Markus Dittrich 
 *
 * This program is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU General Public 
 * License Version 3 as published by the Free Software Foundation. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.
 *
 ****************************************************************/

/* Qt includes */
#include <QCheckBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpinBox>
#include <QVBoxLayout>

/* local includes */
#include "basic_definitions.h"
#include "histogram_selector_widget.h"



/*****************************************************************
 *
 *
 * class HistogramSelectorWidget
 *
 *
 ****************************************************************/

/*****************************************************************
 *
 * public member functions 
 *
 ****************************************************************/
//---------------------------------------------------------------
// constructor
//---------------------------------------------------------------
HistogramSelectorWidget::HistogramSelectorWidget(QWidget* theParent)
  :
    QDialog(theParent),
    parent_(theParent)
{
  status_ = SUCCESSFULLY_CONSTRUCTED;
}


//----------------------------------------------------------------
// main initialization function
//----------------------------------------------------------------
bool HistogramSelectorWidget::Init()
{
  if ( status_ != SUCCESSFULLY_CONSTRUCTED )
  {
    return false;
  }

  setWindowTitle(tr("Histogram"));

  /* create widget layout */
  numBinsSelector_ = new QSpinBox(this); 
  numBinsSelector_->setRange(1,NUM_MAX_HISTOGRAM_BINS);
  numBinsSelector_->setValue(NUM_DEFAULT_HISTOGRAM_BINS);
  QLabel* numBinsLabel = new QLabel(tr("select number of bins"));
  normalizeCheckBox_ = new QCheckBox(tr("normalize"));

  QHBoxLayout* numBinsLayout = new QHBoxLayout;
  numBinsLayout->addWidget(numBinsLabel);
  numBinsLayout->addWidget(numBinsSelector_);
  numBinsLayout->addWidget(normalizeCheckBox_);

  /* buttons */
  QPushButton* createButton = new QPushButton(
      tr("create"),this);
  QPushButton* cancelButton = new QPushButton(
      tr("cancel"),this);

  QHBoxLayout* buttonLayout = new QHBoxLayout;
  buttonLayout->addWidget(createButton);
  buttonLayout->addWidget(cancelButton);
  
  connect(cancelButton,SIGNAL(clicked()),this,SLOT(close()));
  connect(createButton,SIGNAL(clicked()),this,
      SLOT(prepare_histogram_()));
  connect(this,SIGNAL(create_histogram(int, bool)),parent_,
      SLOT(generate_histogram(int, bool)));

  /* create main layout */
  QVBoxLayout* mainLayout = new QVBoxLayout;
  mainLayout->addLayout(numBinsLayout);
  mainLayout->addLayout(buttonLayout);
  
  setLayout(mainLayout);

  return true;
}


/*****************************************************************
 *
 * private slots
 *
 ****************************************************************/

//----------------------------------------------------------------
// slot responsible for preparing and triggering a signal to
// generate a histogram. close widget once we're done.
//----------------------------------------------------------------
void HistogramSelectorWidget::prepare_histogram_()
{
  int numBins = numBinsSelector_->value();
  bool normalize = normalizeCheckBox_->isChecked();
  emit create_histogram(numBins, normalize);
  close();
}


